<?php
/**
 * Created by PhpStorm.
 * User: dyane
 * Date: 2018/12/25
 * Time: 15:46
 */

namespace app\admin\controller;


use think\Db;
use think\Exception;

class Userorder extends Base
{
    /**
     * 用户订单列表页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $users = Db::name('users')->field('id,user_name')->order('id desc')->select();
        $this->assign('users',$users);
        return view();
    }

    /**
     * 订单列表接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrderList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $where = [
            'users_order.is_delete'=>0
        ];

        if(isset($param['status']) && !empty($param['status'])){
            $where['users_order.status'] = $param['status'];
        }
        if(isset($param['user_id']) && !empty($param['user_id'])){
            $where['users_order.user_id'] = $param['user_id'];
        }

        $start = ($page - 1) * $pageSize;
        $list = Db::table('users_order')
            ->where($where)
            ->join('users','users.id=users_order.user_id','left')
            ->join('order_server_cate','order_server_cate.id=users_order.cate_id','left')
            ->field('users_order.order_num,users.mobile,users.user_name,users_order.id,users_order.status,users_order.add_time,users_order.update_time,order_server_cate.cate_name')
            ->order('users_order.update_time desc')
            ->limit($start,$pageSize)
            ->select();
        $count = Db::table('users_order')
            ->where($where)
            ->join('order_server_cate','order_server_cate.id=users_order.cate_id','left')
            ->join('users','users.id=users_order.user_id','left')
            ->count();
        if(count($list) > 0){
            foreach ($list as $k =>$v){
                $list[$k]['add_time'] = date('Y-m-d H:i:s',$list[$k]['add_time']);
                $list[$k]['update_time'] = date('Y-m-d H:i:s',$list[$k]['update_time']);
            }
        }
        return json(['code'=>0,'data'=>$list,'count'=>$count]);
    }


    /**
     * 订单详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_detail()
    {
        $param = $this->request->param();
        // 厂商信息
        $where = [
            'users_certification.status'=>3,
            'users_certification.is_delete'=>0
        ];
        $company = Db::table('users_certification')
            ->where($where)
            ->join('users','users.id=users_certification.user_id','left')
            ->field('users_certification.*,users.id')
            ->select();
        $this->assign('company',$company);

        // 订单code
        $info = Db::table('users_order')
            ->join('order_server_cate','order_server_cate.id=users_order.cate_id','left')
            ->join('system_province','system_province.id=users_order.province_id','left')
            ->join('system_city','system_city.id=users_order.city_id','left')
            ->join('system_area','system_area.id=users_order.area_id','left')
            ->join('users','users_order.accept_company_id=users.id','left')
            ->where('users_order.id',$param['id'])
            ->field('users_order.*,system_province.province,system_city.city,system_area.area,order_server_cate.cate_name,users.user_name,order_server_cate.code')
            ->find();
        $info['add_time'] = date('Y-m-d H:i:s',$info['add_time']);
        if( $info['accept_time']){
            $info['accept_time'] = date('Y-m-d H:i:s',$info['accept_time']);
        }
        $code = Db::table('user_order_code_r')->where('user_order_id',$info['id'])->select();
        $this->assign('info',$info);
        $this->assign('code',$code);
        return $this->fetch('Userorder/order_detail');
    }

    /**
     * 修改订单为完成 保存厂商信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save_order()
    {
        $param = $this->request->param();

        $data = [
            'accept_company_id'=>$param['accept_company_id'],
            'accept_time'=>time(),
            'status'=>3,
        ];

        $info = Db::table('users_order')->where('id',$param['id'])->find();
        $user = Db::table('users_certification')
            ->where('users_certification.user_id',$param['accept_company_id'])
            ->join('users','users.id=users_certification.user_id','left')
            ->field('users_certification.*')
            ->find();
        try{
            Db::startTrans();
            Db::table('users_order')->where('id',$param['id'])->update($data);
            Db::table('system_massages')->insert([
                'title'=> '订单已下单',
                'content'=> '您的订单号'.$info['order_num'].'已下单,接单厂商为'.$user['company_name'],
                'add_time'=> time(),
                'status'=> 0,
                'is_delete'=> 0,
                'type'=> 2,
                'user_id'=> $info['user_id'],
            ]);
            Db::commit();
            return json(['code'=>200,'msg'=>'操作成功']);
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }


    /**
     * 更改订单为处理中
     * @return \think\response\Json
     */
    public function changeStatusDealing()
    {
        $param = $this->request->param();
        try{
            Db::startTrans();
            $info =  Db::table('users_order')->where('id',$param['id'])->find();
            Db::table('users_order')->where('id',$param['id'])->update(['status'=>2,'update_time'=>time()]);
            Db::table('system_massages')->insert([
                'title'=> '订单已下单',
                'content'=> '您的订单号'.$info['order_num'].'正在处理当中请耐心等待',
                'add_time'=> time(),
                'status'=> 0,
                'is_delete'=> 0,
                'type'=> 2,
                'user_id'=> $info['user_id'],
            ]);
            Db::commit();
            return json(['code'=>200,'msg'=>'操作成功']);
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }
}